<?php
require("libraries/agency.php");
class Cruises extends Agency{
	protected $_user = "";
	protected $_name = "";
	public function __construct(){
		parent::__construct();
		$this->load->helper(array('form','url'));
		$this->load->library('form_validation');
		$this->load->model("model_cruises");
		$this->load->helper('mail_helper');
		$this->load->library("string");
		$this->_user = $this->session->userdata('ses_userid');
		$this->_name = $this->session->userdata('ses_user');
	}
	public function index(){
		$data['title'] = "Manage your cruises!";
		$data['template'] = "cruises/listall";
		$data['data'] = "";
		$data['act'] = 3;
		$data['listall'] = $this->model_cruises->listcruis($this->_user);
		$this->load->view("layout",$data);
	}
	public function update(){
		$id = (int)$this->uri->segment(4);
		$data['template'] = "cruises/edit";
		$data['data'] = "";$data['act'] = 3;
		$data['getdata'] = $this->model_cruises->getdata($id);
		//$this->debug($data['getdata']);
		$data['title'] = "Edit cruise - ".$data['getdata']['sheep_name'];
		$listcrs = $this->model_cruises->listcruis($this->_user);
		$users = array();
		foreach($listcrs as $cruise){
			$users[] = $cruise['cruises_id'];
		}
		if(!in_array($id,$users)){
			die("Page not found");
		}
		if($data['getdata'] == NULL){
			redirect(base_url()."agency/index");
		}
		$albid = $data['getdata']['album_id'];
		$data['listimg'] = $this->model_cruises->gallery($albid);
		if(isset($_POST['ok'])){
			$frienly = str_replace(' ','-',strtolower($this->string->replace($this->input->post('sheep_name'))));
			$db = array(
				"name" => $_POST['sheep_name'],
				"rewrite" => $frienly,
				"adress"  => $_POST['sheep_ltinerary'],
				"phone" => $_POST['sheep_phone'],
				"email" => $_POST['sheep_email'],
				"website" => $_POST['sheep_site'],
				"order" => $_POST['sheep_order'],
				"pricepro" => $_POST['sheep_price'],
				"pricenol" => $_POST['sheep_price_old'],
				"keywords" => $_POST['sheep_keys'],
				"description" => $_POST['sheep_des'],
				"status" => $_POST['sheep_status'],
				"duration1" => $this->input->post("sheep_tour1"),
				"duration2" => $this->input->post("sheep_tour2"),
				"duration3" => $this->input->post("sheep_tour3"),
				"duration4" => $this->input->post("sheep_tour4"),
				"duration5" => $this->input->post("sheep_tour5"),
				"sheetlink" => $_POST['sheep_fact'],
				"menulink" => $_POST['sheep_menu'],
				"drinklink" => $_POST['sheep_drink'],
				"youtlink" => $_POST['sheep_video'],
				"shortinfo" => $_POST['sheep_info'],
				"include" => $_POST['sheep_include'],
				"oneday" => $_POST['sheep_1day'],
				"twoday" => $_POST['sheep_2day'],
				"threeday" => $_POST['sheep_3day'],
				"fourday" => $_POST['sheep_4day'],
				"fiveday" => $_POST['sheep_5day'],
				"cruiseid" => $id,
				"agencyid" => $this->_user,
				"datetime" => date("d/m/Y - H:i:s")
			); 
			if($_FILES['sheep_logo']['name'] != NULL){
				$config['upload_path'] = './uploads/sheep/logo';
				$config['allowed_types'] = '*';
				$config['max_size']	= '3000';
				$config['max_width']  = '2704';
				$config['max_height']  = '2700';
				$this->load->library('upload',$config);
				if(!$this->upload->do_upload("sheep_logo")){
					$data = array('error' => $this->upload->display_errors());
					$data['template'] = "cruises/edit";
					$data['data'] = "";$data['act'] = 3;
					$data['getdata'] = $this->model_cruises->getdata($id);
					$albid = $data['getdata']['album_id'];
					$data['listimg'] = $this->model_cruises->gallery($albid);
					$data['title'] = "Edit cruise - ".$data['getdata']['sheep_name'];
					$listcrs = $this->model_cruises->listcruis($this->_user);
					$this->load->view("layout",$data);
					return FALSE;
				}else{
					$data = $this->upload->data();
					$db['logo'] = $data['file_name'];
					$this->createThumb($db['logo'],"sheep/logo","100","90");
				}
			 }
			if($_FILES['sheep_image']['name'] != NULL){
				$configs['upload_path']   = './uploads/sheep/logo';
				$configs['allowed_types'] = 'jpg|png|jpeg|gif';
				$configs['max_size']	= '2500';
				$configs['max_width']  = '2524';
				$configs['max_height']  = '2524';
				$this->load->library('upload',$configs);
				if(!$this->upload->do_upload("sheep_image")){
					$data = array('error' => $this->upload->display_errors());
					$data['template'] = "cruises/edit";
					$data['data'] = "";$data['act'] = 3;
					$data['getdata'] = $this->model_cruises->getdata($id);
					$albid = $data['getdata']['album_id'];
					$data['listimg'] = $this->model_cruises->gallery($albid);
					$data['title'] = "Edit cruise - ".$data['getdata']['sheep_name'];
					$listcrs = $this->model_cruises->listcruis($this->_user);
					$this->load->view("layout",$data);
					return FALSE;
				}else{
					$data = $this->upload->data();
					$db['slideimg'] = $data['file_name'];
					$this->createThumb($db['slideimg'],"sheep/logo","140","140");
				}
			}
			if($_FILES['sheep_image_thumb']['name'] != NULL){
				$config['upload_path']   = './uploads/sheep/logo';
				$config['allowed_types'] = 'jpg|png|jpeg|gif';
				$config['max_size']	= '2500';
				$config['max_width']  = '2524';
				$config['max_height']  = '2524';
				$this->load->library('upload',$config);
				if(!$this->upload->do_upload("sheep_image_thumb")){
					$data = array('error' => $this->upload->display_errors());
					$data['template'] = "cruises/edit";
					$data['data'] = "";$data['act'] = 3;
					$data['getdata'] = $this->model_cruises->getdata($id);
					$albid = $data['getdata']['album_id'];
					$data['listimg'] = $this->model_cruises->gallery($albid);
					$data['title'] = "Edit cruise - ".$data['getdata']['sheep_name'];
					$listcrs = $this->model_cruises->listcruis($this->_user);
					$this->load->view("layout",$data);
					return FALSE;
				}else{
					$data = $this->upload->data();
					$db['imgthumb'] = $data['file_name'];
					$this->createThumbs($db['imgthumb'],"sheep/logo",300,200);
				}
			}
			$this->sendmail($this->_name,$data['getdata']['sheep_name']);
			$this->model_cruises->add($db);
			redirect(base_url()."agency/cruises/delay");
		}
		$this->load->view("layout",$data);
	}
	public function delay(){
		$data['template'] = "cruises/delay";
		$data['data'] = "";
		$data['title'] = "Your update has been changed";
		$this->load->view("layout",$data);
	}
	public function sendmail($agency,$cruise,$file=''){
		   $mesnger = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
				<html>
				<head>
				<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
				<style type="text/css">
					body{font:12px Verdana, Geneva, sans-serif;}
				</style>
				</head>
				<body>
					<h3 style="font-size:16px;">AGENCY UPDATE CRUISES</h3>
					<p>You have a message from bestcruiseshalong.com please check and confirm for your partners</p>
					<p>Cruise name : '.$cruise.'</p>
					<p>Agency name : '.$agency.'</p>
				<br/>
            ';
		  $mesnger.= "<body></html>";
		  send_mail_helper('haanhdon@gmail.com','Confirm for your partners',htmlspecialchars_decode($mesnger),$file);
	}
	public function createThumb($fileName,$path,$width,$height){
		$this->load->library('image_lib');
		//$this->load->helper('thumbnail_helper');
		$config['image_library'] = 'gd2';
		$config['source_image'] = 'uploads/'.$path.'/'.$fileName;
		$config['new_image']    = 'uploads/'.$path.'/thumb/'.$fileName;
		$config['create_thumb'] = TRUE;
		//$config['maintain_ratio'] = TRUE;
		$config['thumb_marker'] = FALSE;
		//$config['maintain_ratio'] = FALSE;
		$config['width'] = $width;
		$config['height'] = $height;
		$this->image_lib->initialize($config); 
		$this->image_lib->resize();
		$this->image_lib->clear();
	}
	public function createThumbs($fileName,$path,$width,$height){
		$this->load->library('image_lib');
		//$this->load->helper('thumbnail_helper');
		$config['image_library'] = 'gd2';
		$config['source_image'] = 'uploads/'.$path.'/'.$fileName;
		$config['new_image']    = 'uploads/'.$path.'/thumbs/'.$fileName;
		$config['create_thumb'] = TRUE;
		//$config['maintain_ratio'] = TRUE;
		$config['thumb_marker'] = FALSE;
		//$config['maintain_ratio'] = FALSE;
		$config['width'] = $width;
		$config['height'] = $height;
		$this->image_lib->initialize($config); 
		$this->image_lib->resize();
		$this->image_lib->clear();
	}
}